package com.salt.modules.sys.web;

import com.salt.common.json.AjaxJson;
import com.salt.core.log.annotation.OperationRecord;
import com.salt.core.web.BaseController;
import com.salt.modules.sys.entity.AppChildMenu;
import com.salt.modules.sys.entity.AppMenu;
import com.salt.modules.sys.entity.Menu;
import com.salt.modules.sys.entity.User;
import com.salt.modules.sys.service.AppChildMenuService;
import com.salt.modules.sys.service.AppMenuService;
import com.salt.modules.sys.utils.UserUtils;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 应用菜单(子级菜单)Controller
 * @Author salt
 * @Date 2022/1/18
 * @Version 1.0.1
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/menu/app/child")
public class AppChildMenuController extends BaseController {

    private final AppChildMenuService appChildMenuService;
    private final AppMenuService appMenuService;

    @PostMapping("save")
    @OperationRecord(service = "系统服务", module = "应用菜单(子级菜单)", operation = "新增/修改", description = "新增/修改应用菜单(子级菜单)")
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson save(Menu menu) {
        User user = UserUtils.getUser();
        String parentId = menu.getParentIds().split(",")[2];
        AppChildMenu appChildMenu = new AppChildMenu(user.getId(), menu.getId(), parentId, menu.getTenant());
        AppChildMenu appchildmenu = appChildMenuService.find(appChildMenu);
        if (appchildmenu == null) {
            AppMenu appMenu = new AppMenu(user.getId(), parentId, menu.getTenant());
            AppMenu appmenu = appMenuService.find(appMenu);
            if (appmenu == null) {
                appMenuService.save(appMenu);
            }
            appChildMenuService.save(appChildMenu);
            return AjaxJson.success("下载成功");
        }
        return AjaxJson.error(menu.getName() + "已下载!");
    }

    @DeleteMapping("delete")
    @OperationRecord(service = "系统服务", module = "应用菜单(子级菜单)", operation = "删除", description = "删除应用菜单(子级菜单)")
    public AjaxJson delete(AppChildMenu appChildMenu) {
        appChildMenuService.delete(appChildMenu);
        return AjaxJson.success("删除成功!");
    }
}
